<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'pay_status_text',
        'ship_status_text',
        'status_text',
        'confirm_text',
        'confirmtime_text'
    ];
    

    
    public function getPayStatusList()
    {
        return ['1' => __('Pay_status 1'), '2' => __('Pay_status 2'), '3' => __('Pay_status 3')];
    }

    public function getShipStatusList()
    {
        return ['1' => __('Ship_status 1'), '2' => __('Ship_status 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '9' => __('Status 9')];
    }

    public function getConfirmList()
    {
        return ['1' => __('Confirm 1'), '2' => __('Confirm 2')];
    }


    public function getPayStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['pay_status'] ?? '');
        $list = $this->getPayStatusList();
        return $list[$value] ?? '';
    }


    public function getShipStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['ship_status'] ?? '');
        $list = $this->getShipStatusList();
        return $list[$value] ?? '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getConfirmTextAttr($value, $data)
    {
        $value = $value ?: ($data['confirm'] ?? '');
        $list = $this->getConfirmList();
        return $list[$value] ?? '';
    }


    public function getConfirmtimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['confirmtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setConfirmtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function jieziarea()
    {
        return $this->belongsTo('Jieziarea', 'ship_area_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
